package com.uenpay.im.ui.old.activity.company;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-3-5 13:51:38
 * @copyRight http://www.qiyunxin.com
 */
public class OnlyCompanyDeparListActivity extends Activity {

	private ListView listView;
	private CompanyHandle mCompanyHandle = new CompanyHandle();
	private DepartmentsAdapter mDepartmentsAdapter = null;
	private View loading;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		IMApplication.getInstance().addActivity(this);
		setContentView(R.layout.activity_list_layout);
		initView();
		initListener();
		int departid = getIntent().getIntExtra("departid", 0);
		int entid = getIntent().getIntExtra("entid", 0);
		getData(departid, entid);
	}

	private void initView() {
		((TextView) findViewById(R.id.title_textview))
				.setText(R.string.choose_department);
		listView = (ListView) findViewById(R.id.listview);
		loading = findViewById(R.id.loading);
		((TextView) findViewById(R.id.title_right_tv)).setText(R.string.sure);
		findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
	}

	private void initListener() {
		findViewById(R.id.back_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						finish();
					}
				});
		findViewById(R.id.title_right_layout).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if(mDepartmentsAdapter != null){
					DepartmentEntity mDepartmentEntity = mDepartmentsAdapter.getSelectedDepart();
					if(mDepartmentEntity != null){
						Intent intent = new Intent();
						intent.putExtra("departId", mDepartmentEntity.departid);
						intent.putExtra("departName",
								mDepartmentEntity.departname);
						setResult(RESULT_OK, intent);
						finish();
					}
				}
			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				DepartmentEntity mDepartmentEntity = (DepartmentEntity) arg0
						.getAdapter().getItem(arg2);
				if (mDepartmentEntity != null) {
					if (mDepartmentEntity.isleaf == 0) {
						Intent intent = new Intent(
								OnlyCompanyDeparListActivity.this,
								OnlyCompanyDeparListActivity.class);
						intent.putExtra("entid", mDepartmentEntity.entid);
						intent.putExtra("departid", mDepartmentEntity.departid);
						startActivityForResult(intent, 100);
					} else {
						mDepartmentsAdapter.updateSelectedDepart(mDepartmentEntity);
//						Intent intent = new Intent();
//						intent.putExtra("departId", mDepartmentEntity.departid);
//						intent.putExtra("departName",
//								mDepartmentEntity.departname);
//						setResult(RESULT_OK, intent);
//						finish();
					}
				}
			}

		});
	}

	private void getData(int departid, int entid) {
		loading.setVisibility(View.VISIBLE);
		mCompanyHandle.getDepartByPidList(departid, entid,
				new CompanyHandle.IGetDepartListResult() {

					@Override
					public void onGetDepartResult(int status, String msg,
							ArrayList<DepartmentEntity> arrayList) {
						loading.setVisibility(View.GONE);
						if (status == HttpInvokeResultCode.RESULT_OK) {
							if (arrayList != null && arrayList.size() > 0) {
								mDepartmentsAdapter = new DepartmentsAdapter(
										OnlyCompanyDeparListActivity.this,
										arrayList,true);
								listView.setAdapter(mDepartmentsAdapter);
							}
						}
					}
				});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 100 && resultCode == RESULT_OK) {
			int departid = data.getIntExtra("departId", 0);
			String departName = data.getStringExtra("departName");
			Intent intent = new Intent();
			intent.putExtra("departId", departid);
			intent.putExtra("departName", departName);
			setResult(RESULT_OK, intent);
			finish();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onBackPressed() {
		finish();
		super.onBackPressed();
	}

	@Override
	protected void onDestroy() {
		IMApplication.getInstance().removeActivity(this);
		super.onDestroy();
	}
}
